﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEngine;
using UnityEngine.UI;

public class Util {

	public static string MD5(string filePath)
    {
        try
        {
            FileStream file = new FileStream(filePath, FileMode.Open);
            System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
            byte[] retVal = md5.ComputeHash(file);
            file.Close();

            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < retVal.Length; i++)
            {
                sb.Append(retVal[i].ToString("x2"));
            }
            return sb.ToString();
        }
        catch (Exception ex)
        {
            throw new Exception("GetMD5HashFromFile() fail,error:" + ex.Message);
        }
    }

    public static byte[] ReadAllBytes(string path)
    {
        byte[] luaByte = null;
        try
        {
            luaByte = File.ReadAllBytes(path);
        }
        catch
        {
            return null;
        }
        return luaByte;
    }

    public static string[] ReadAllLines(string path)
    {
        string[] texts = null;
        try
        {
            texts = File.ReadAllLines(path);
        }
        catch
        {
            return null;
        }
        return texts;
    }

    public static string PathGetExtension(string path)
    {
        return Path.GetExtension(path); 
    }

    public static string PathGetFileName(string path)
    {
        return Path.GetFileName(path);
    }

    public static bool FileExists(string path)
    {
        return File.Exists(path);
    }

    public static bool IsNull(UnityEngine.Object target)
    {
        return target == null;
    }

    private static Material _uiGrayMaterial;
    private static Material uiGrayMaterial
    {
        get
        {
            if (_uiGrayMaterial == null)
            {
                Shader shader = Shader.Find("Custom/UI-Default-Gray");
                _uiGrayMaterial = new Material(shader);
            }
            return _uiGrayMaterial;
        }
    }
    public static void SetUIGray(Image img,bool b)
    {
        if (b)
            img.material = uiGrayMaterial;
        else
            img.material = null;
    }
}
